Imports System.Text
Namespace DotNetNuke.Modules.ActiveForums
    Public Class Polls
        Public Function PollResults(ByVal TopicId As Integer, ByVal ImagePath As String) As String
            Dim BarWidth As Integer = 275
            Dim sb As New StringBuilder
            sb.Append("<table width=""80%"" align=""center"" cellpadding=""4"" cellspacing=""0"" class=""afpollresults"">")
            Dim dr As IDataReader
            dr = DataProvider.Instance.Poll_GetResults(TopicId)
            dr.Read()
            sb.Append("<tr><td colspan=""2"" class=""afnormal""><b>")
            sb.Append(CType(dr("Question"), String))
            sb.Append("</b></td></tr>")
            dr.NextResult()
            dr.Read()
            Dim VoteCount As Double
            VoteCount = CType(dr(0), Double)
            dr.NextResult()
            While dr.Read
                Dim dblPercent As Double = 0
                If VoteCount <> 0 Then
                    dblPercent = Convert.ToDouble(CType(dr("ResultCount"), Double) / VoteCount)
                End If
                sb.Append("<tr><td class=""afnormal""><b>")
                sb.Append(CType(dr("OptionName"), String) & "</b> (" & CType(dr("ResultCount"), String) & ")")
                sb.Append("</td></tr><tr><td class=""afnormal"">")
                sb.Append("<span class=""afpollbar"">")
                sb.Append("<img src=""" & ImagePath & "/spacer.gif"" height=""10"" width=""" & CInt((BarWidth * dblPercent)) & """ />")
                sb.Append("</span>&nbsp;" & CInt(dblPercent * 100).ToString & "%")
                sb.Append("</td></tr>")
            End While
            sb.Append("</table>")
            Return sb.ToString
        End Function

        Protected Friend Function HasVoted(ByVal TopicId As Integer, ByVal UserId As Integer) As Boolean
            Try
                Dim iVote As Integer = DataProvider.Instance.Poll_HasVoted(TopicId, UserId)
                If iVote > 0 Then
                    Return True
                Else
                    Return False
                End If

            Catch ex As Exception
                Return False
            End Try
        End Function

    End Class

End Namespace

